** Code for tables in The Effects of Changes in Local Bank Health on Household Consumption
** by Daniel Cooper and Joe Peek, ReStat 2020
/* ////////////////////////////////////////////////////////////////////////// */
clear all
set more off
set maxvar 120000
set scheme s1color

/* This code creates tables 2-A10 in the paper. It requires the output from maindata.do

Results controlling for income constraint and LA holdings (tables 3-A10) are created in a 
somewhat complicated procedure where each regression is re-run twice (or four times for 
the contraint/LA splits) and the variable names (not labels) are switched so they will 
appear in the same row of the final table. This is purely for presentation purposes.
See further comments the table 4 block below */

local version revision_hmr
local dataname main_dataset_revision_hmr
local filename "tables_final"
local camname "tables_final"

* update the locals below with the relevant paths
local data_dir /shared/Joe_Peek/bankhealth_nocamels/data
local tex_dir /shared/Joe_Peek/bankhealth_nocamels/final_tables

* The code is divided into blocks by table for ease of execution
* the "c" tables are the main tables in the text (in order) and the "a" tables
* are the appendix tables.
********************************************************************************
scalar core = 0
	// see equifax.do for table 1
	scalar c2 = 0
		scalar a3full = 0 // alt table 2 with all variables presented is table A.3
	scalar c3 = 0
	scalar c4 = 0
	scalar c5 = 0
	scalar c6 = 0
	scalar c7 = 0
	
scalar apx = 0
	scalar a1 = 0
	scalar a2 = 0
	// see a3full above, bc a3 is a longer version of table 2
	scalar a4 = 0
	scalar a5 = 0
	scalar a6 = 0
	scalar a7 = 0
	scalar a8 = 0
	scalar a9 = 0
	scalar a10 = 1
********************************************************************************

#delimit ;
/* /////////////////////////////////////////////////////////////////////////////// */
* Variable Locals ;
* constraint, liquid asset interactions with regression vars, needed for regression structure ;
/* /////////////////////////////////////////////////////////////////////////////// */
local t _a;
/* ////////////////////////// */
** ML BANK HEALTH  ;
foreach v in re npl { ;
	if "`v'"=="re" local u "" ;
	if "`v'"=="npl" local u "npl" ;

local bh`u'_lm `v'_a_50_5 ;
	local bh`u'10_lm `v'_a_50_10 ;
	
* Constrained ;
local bh`u'_vars_lm_const `bh`u'_lm'm_bhc_ev_u `bh`u'_lm'm_bhc_ev_c ;
* LA Splits ;
foreach w in negdev 5 { ;
	if "`w'" == "negdev" local ver;
	if "`w'" == "5" local ver _5;
		
local bh`u'_vars_lm_lw_30`ver' `bh`u'_lm'm_bhc_ev_ul_30`ver' `bh`u'_lm'm_bhc_ev_cl_30`ver' `bh`u'_lm'm_bhc_ev_uh_30`ver' `bh`u'_lm'm_bhc_ev_ch_30`ver' ;
	local bhnpl10_vars_lm_lw_30`ver' `bhnpl10_lm'm_bhc_ev_ul_30`ver' `bhnpl10_lm'm_bhc_ev_cl_30`ver' `bhnpl10_lm'm_bhc_ev_uh_30`ver' `bhnpl10_lm'm_bhc_ev_ch_30`ver' ;

if "`v'"=="npl" { ;
local bhnpl_vars_lm_lw_30`ver' `bhnpl_lm'm_bhc_ev_ul_30`ver' `bhnpl_lm'm_bhc_ev_cl_30`ver' `bhnpl_lm'm_bhc_ev_uh_30`ver' `bhnpl_lm'm_bhc_ev_ch_30`ver' ;
} ;

} ;
} ;

/* ////////////////////////// */
* BH x VOLATILITY ;
foreach v in re npl { ;
	if "`v'"=="re" local u "" ;
	if "`v'"=="npl" local u "npl" ;

local vola`u' vl_`v'_a_50_5mbhcev ;
if "`v'"=="npl" { ;
	local volanpl10 vl_npl_a_50_10mbhcev ;
} ;

* Constrained ;
local vola`u'_const vl_`v'_a_50_5mbhcev_u vl_`v'_a_50_5mbhcev_c ;

* LA splits ;
foreach w in negdev 5 { ;
	if "`w'" == "negdev" local ver;
	if "`w'" == "5" local ver _5;
	
	local vola`u'_lw_30`ver' vl_`bh`u'_lm'mbhcev_ul_30`ver' vl_`bh`u'_lm'mbhcev_cl_30`ver' vl_`bh`u'_lm'mbhcev_uh_30`ver' vl_`bh`u'_lm'mbhcev_ch_30`ver' ;
	
	if "`v'"=="npl" { ;
	local vola`u'10_lw_30`ver' vl_`bh`u'10_lm'mbhcev_ul_30`ver' vl_`bh`u'10_lm'mbhcev_cl_30`ver' vl_`bh`u'10_lm'mbhcev_uh_30`ver' vl_`bh`u'10_lm'mbhcev_ch_30`ver' ;

	local vola`u'_lw_30`ver' vl_`bh`u'_lm'mbhcev_ul_30`ver' vl_`bh`u'_lm'mbhcev_cl_30`ver' vl_`bh`u'_lm'mbhcev_uh_30`ver' vl_`bh`u'_lm'mbhcev_ch_30`ver' ;
	} ;
} ;
} ;

/* ////////////////////////// */
* BH Quintiles ;
foreach v in bhq vlbhq { ;
	if "`v'"=="bhq" { ;
		local u "" ;
		local w "" ;
	} ;
	
	if "`v'"=="vlbhq" { ;
		local u "vl" ;
		local w "vl_" ;
	} ;

	local `u'mlnpl50qn `w'qn2_npl_a_50_5mbhcev `w'qn3_npl_a_50_5mbhcev `w'qn4_npl_a_50_5mbhcev `w'qn5_npl_a_50_5mbhcev ;
	local `u'mlnpl50qn_lw_30 `w'qn2_npl_a_50_5mbhcevul_30 `w'qn2_npl_a_50_5mbhcevcl_30 `w'qn2_npl_a_50_5mbhcevuh_30 `w'qn2_npl_a_50_5mbhcevch_30 `w'qn3_npl_a_50_5mbhcevul_30 `w'qn3_npl_a_50_5mbhcevcl_30 `w'qn3_npl_a_50_5mbhcevuh_30 `w'qn3_npl_a_50_5mbhcevch_30 `w'qn4_npl_a_50_5mbhcevul_30 `w'qn4_npl_a_50_5mbhcevcl_30 `w'qn4_npl_a_50_5mbhcevuh_30 `w'qn4_npl_a_50_5mbhcevch_30 `w'qn5_npl_a_50_5mbhcevul_30 `w'qn5_npl_a_50_5mbhcevcl_30 `w'qn5_npl_a_50_5mbhcevuh_30 `w'qn5_npl_a_50_5mbhcevch_30 ;
	local `u'mlnpl50qn_c `w'qn2_npl_a_50_5mbhcevc `w'qn3_npl_a_50_5mbhcevc `w'qn4_npl_a_50_5mbhcevc `w'qn5_npl_a_50_5mbhcevc ;
} ;

*INCOME;
local inc ln_famincrat;
	local inc_const ln_famincrat_re_a_u ln_famincrat_re_a_c;
	local inc_lw_5 ln_famincrat_re_a_ul_5 ln_famincrat_re_a_cl_5 ln_famincrat_re_a_uh_5  ln_famincrat_re_a_ch_5;
	local inc_lw_30 ln_famincrat_re_a_ul_30 ln_famincrat_re_a_cl_30 ln_famincrat_re_a_uh_30  ln_famincrat_re_a_ch_30;
	local inc_lw_30_5 ln_famincrat_re_a_ul_30_5 ln_famincrat_re_a_cl_30_5 ln_famincrat_re_a_uh_30_5  ln_famincrat_re_a_ch_30_5;
	
* OTHER CONTROLS;
local hh_control ageh age2 famsize famsizessq m_se m_de;
local macro_control hp_ev_w1 bartik_ebev_w1;

* Total wealth;
local totwealth totwr_100;
	local totwealth_const totwr_100_re_a_u totwr_100_re_a_c ;
	local totwealth_lw_30 totwr_100_re_a_uh_30 totwr_100_re_a_ul_30 totwr_100_re_a_ch_30 totwr_100_re_a_cl_30;	
	local totwealth_lw_30_5 totwr_100_re_a_uh_30_5 totwr_100_re_a_ul_30_5 totwr_100_re_a_ch_30_5 totwr_100_re_a_cl_30_5;	
	
* Total Assets, Debt;
local totasset totar_100;
local totdebt totdr_100;

* CAMELS versions ;
foreach var in ln_famincrat_cam cam_wa_ev vl_cam_wa_ev totwr_100_cam { ;
	if "`var'" == "ln_famincrat_cam" local l inc_cam;
	if "`var'" == "totwr_100_cam" local l totwealth_cam;
	if "`var'" == "cam_wa_ev" local l bh_vars_cam;
	if "`var'" == "vl_cam_wa_ev" local l vola_cam;
	
	local `l'_lw_30 `var'_ul_30 `var'_cl_30 `var'_uh_30 `var'_ch_30 ;
} ;
/* /////////////////////////////////////////////////////////////////////////////// */
cd "`tex_dir'" ;
* See the text for an explanation of the basic regression set up ;

* CORE TABLES (2-7) ;
if core { ;
* Table 2: BH and Consumption: Initial Results ;
* Table A3: version of table 2 with all regressors ;
if c2 { ;
preserve ;
use `data_dir'/`dataname'_hw_lw_new_30_15.dta, clear;
drop if volainc == . ;

** Col 1: No BH; 
eststo A_nobh, title("Food"): reghdfe ln_psidc_food `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1, absorb(unique location_id year) vce(cluster location_id);

* Obs. ;
count if e(sample);
local total_A_nobh = r(N);

** Col 2: Overall NPL BH;
eststo A_2, title("Food"): reghdfe ln_psidc_food npl_a_50_ev vl_npl_a_50_ev `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1, absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_ev + vl_npl_a_50_ev*`yvol' ;
eststo a2_lc ;

* Obs. ;
count if e(sample);
local total_A_2 = r(N);

** Col 3: ML NPL BH; 
eststo A_3, title("Food"): reghdfe ln_psidc_food npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1, absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev + vl_npl_a_50_5mbhcev*`yvol' ;
eststo a3_lc ;

* Obs. ;
count if e(sample);
local total_A_3 = r(N);

restore;

* Wealth Sample ;

preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;

** Col 4: Weath sample without wealth variables, ML NPL BH;
eststo A_4, title("Food"): reghdfe ln_psidc_food npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1 , absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev + vl_npl_a_50_5mbhcev*`yvol' ;
eststo a4_lc ;

* Obs. ;
count if e(sample);
local total_A_4 = r(N);


** Col 5: FW sample, ML NPL BH + wealth/income ;
eststo A_5, title("Food"): reghdfe ln_psidc_food npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev `totwealth' `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1 , absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev + vl_npl_a_50_5mbhcev*`yvol' ;
eststo a5_lc ;

* Obs. ;
count if e(sample);
local total_A_5 = r(N);

** Col 6: FW sample, ML NPL BH, assets and debt ;
eststo A_6, title("Food"): reghdfe ln_psidc_food npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev `totasset' `totdebt' `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1 , absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev + vl_npl_a_50_5mbhcev*`yvol' ;
eststo a6_lc ;

* Obs. ;
count if e(sample);
local total_A_6 = r(N);

* total BH effect stars ;
esttab a2_lc a3_lc a4_lc a5_lc a6_lc, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* Latex output ;
esttab A_nobh A_2 A_3 A_4 A_5 A_6 using "`tex_dir'/t2_fin.tex", replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) keep(`inc' npl_a_50_ev npl_a_50_5m_bhc_ev vl_npl_a_50_ev vl_npl_a_50_5mbhcev `macro_control' `totwealth' `totasset' `totdebt')
 varwidth(25) modelwidth(6) booktabs
mtitles nodepvars stats(r2_a, fmt(%5.3f) labels("Adj. R-squared"))
title("Bank Health and Consumption: Initial Results") 
order(`inc' `macro_control' npl_a_50_ev vl_npl_a_50_ev npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev `totwealth' `totasset' `totdebt') collabels(none) label long 
substitute("                         &\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}\\" "\cmidrule(lr){2-4} \cmidrule(lr){5-7} & \multicolumn{3}{c}{Full Sample} & \multicolumn{3}{c}{Wealth Sample} \\")
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} & &-1.712** &-1.602***&-1.842***&-1.772***&-1.771***\\ & &(0.766)   &(0.516)   &(0.557)   &(0.551)   &(0.552) \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{1}{c}{`total_A_nobh'\phantom{*}} & \multicolumn{1}{c}{`total_A_2'\phantom{*}} & \multicolumn{1}{c}{`total_A_3'\phantom{*}} & \multicolumn{1}{c}{`total_A_4'\phantom{*}} & \multicolumn{1}{c}{`total_A_5'\phantom{*}} & \multicolumn{1}{c}{`total_A_6'\phantom{*}} \\");

* Table A.3: BH and Consumption: Initial Results, All Regressors ;
if a3full { ;
esttab A_nobh A_2 A_3 A_4 A_5 A_6 using "`tex_dir'/a3_fin.tex", replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) drop(_cons ageh)
 varwidth(25) modelwidth(6) booktabs
mtitles nodepvars stats(r2_a, fmt(%5.3f) labels("Adj. R-squared"))
title("Bank Health and Consumption: Initial Results \\ All Regressors") 
order(`inc' `macro_control' npl_a_50_ev vl_npl_a_50_ev npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev `totwealth' `totasset' `totdebt' `hh_control') collabels(none) label long 
substitute("                         &\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}\\" "\cmidrule(lr){2-4} \cmidrule(lr){5-7} & \multicolumn{3}{c}{Full Sample} & \multicolumn{3}{c}{Wealth Sample} \\")
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} & &-1.712** &-1.602***&-1.842***&-1.772***&-1.771***\\ & &(0.766)   &(0.516)   &(0.557)   &(0.551)   &(0.552) \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{1}{c}{`total_A_nobh'\phantom{*}} & \multicolumn{1}{c}{`total_A_2'\phantom{*}} & \multicolumn{1}{c}{`total_A_3'\phantom{*}} & \multicolumn{1}{c}{`total_A_4'\phantom{*}} & \multicolumn{1}{c}{`total_A_5'\phantom{*}} & \multicolumn{1}{c}{`total_A_6'\phantom{*}} \\");
} ;

restore;

} ;

* Table 3: Controlling for Constrained Households ;
if c3 { ;
preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;

*Col 1: constrained; 
eststo C_1c, title("Const."): reghdfe ln_psidc_food  `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc1c ;

*switch variables;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
		
	tempvar x;
	g `x' = `var'_u;
	replace `var'_u = `var'_c;
	replace `var'_c = `x';
};

*Col 1: unconstrained; 
eststo C_1u, title("Unconst."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%5.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc1u ;

restore;

*NEGATIVE INCOME DEVIATION >/5% OR < 30;

* WEALTH SAMPLE ;
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;

*switch 5% and age_cut variables with regular constrained so can stay in same row as _c; 
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
	
	replace `var'_u = `var'_u30;
	replace `var'_c = `var'_c30;

};

*Col 2: constrained; 
eststo C_2c, title("Const."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc2c ;

*switch variables;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
			
	tempvar x;
	g `x' = `var'_u;
	replace `var'_u = `var'_c;
	replace `var'_c = `x';
	
};

*Col 2: unconstrained; 
eststo C_2u, title("Unconst."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

*R2;
local r_C_2 = string(e(r2_a), "%5.3f");

*Obs.;
count if e(sample);
local total_C_2 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc2u ;

restore ;
/* Low Liq Wealth */
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;
drop if `volanpl'_u30_5 == . ;

*switch Low LW and age_cut variables with regular constrained so can stay in same row as _c; 
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
	
	replace `var'_u = `var'_u_llw;
	replace `var'_c = `var'_c_llw;

};

*Col 3: constrained; 
eststo C_3c, title("Const."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc3c ;

*switch variables;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
			
	tempvar x;
	g `x' = `var'_u;
	replace `var'_u = `var'_c;
	replace `var'_c = `x';
	
};

*Col 3: unconstrained; 
eststo C_3u, title("Unconst."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

*R2;
local r_C_3 = string(e(r2_a), "%5.3f");

* Obs. ;
count if e(sample);
local total_C_3 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc3u ;

restore; 
/* Low Liq Wealth or Under 30 */
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;
drop if `volanpl'_u30_5 == . ;

*switch Low LW and age_cut variables with regular constrained so can stay in same row as _c; 
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
	
	replace `var'_u = `var'_u_llw30;
	replace `var'_c = `var'_c_llw30;

};

*Col 4: constrained; 
eststo C_4c, title("Const."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc4c ;

*switch variables;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' `totwealth'_re_a {;
			
	tempvar x;
	g `x' = `var'_u;
	replace `var'_u = `var'_c;
	replace `var'_c = `x';
	
};

*Col 4: unconstrained; 
eststo C_4u, title("Unconst."): reghdfe ln_psidc_food `bhnpl_vars_lm_const' `volanpl_const' `totwealth_const' `inc_const' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1, absorb(unique location_id year) vce(cluster location_id);

*R2;
local r_C_4 = string(e(r2_a), "%5.3f");

* Obs. ;
count if e(sample);
local total_C_4 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lc4u ;

* total BH effect stars ;
esttab lc1u lc1c lc2u lc2c lc3u lc3c lc4u lc4c , starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

*Regression table latex output;
local outname "t3_fin" ;
esttab C_1u C_1c C_2u C_2c C_3u C_3c C_4u C_4c using "`tex_dir'/`outname'.tex", replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c `totwealth'_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs 
mtitles nodepvars mgroups("Neg. Dev." "Neg. Dev. or $<$ 30" "Low LA" "Low LA or $<$ 30", pattern(1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Controlling for Constrained Households") 
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c `totwealth'_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} &-1.027*  &-2.324***&-0.733   &-2.438***&-1.493*  &-2.494** &-0.911   &-2.383***\\ &(0.587)   &(0.676)   &(0.616)   &(0.625)   &(0.786)   &(0.972)   &(0.588)   &(0.751) \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{2}{c}{`total_C_1'\phantom{*}} & \multicolumn{2}{c}{`total_C_2'\phantom{*}} & \multicolumn{2}{c}{`total_C_3'\phantom{*}} & \multicolumn{2}{c}{`total_C_4'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{2}{c}{`r_C_1'\phantom{*}} & \multicolumn{2}{c}{`r_C_2'\phantom{*}} & \multicolumn{2}{c}{`r_C_3'\phantom{*}} & \multicolumn{2}{c}{`r_C_4'\phantom{*}}\\")
;
* substitute("\BSmidrule" "\BScmidrule(l){2-3} \BScmidrule(l){4-5} \BScmidrule(l){6-7} \BScmidrule(l){8-9}"); 

* add lines between specifications ;
filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("\BSbegin{longtable}{l*{8}{S[table-format=2.5, table-align-text-post=false]}}") 
to("\BSbegin{longtable}{l*{2}{S[table-format=2.5, table-align-text-post=false]}|*{2}{S[table-format=2.5, table-align-text-post=false]}|*{2}{S[table-format=2.5, table-align-text-post=false]}|*{2}{S[table-format=2.5, table-align-text-post=false]}}") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("                         &\BSmulticolumn{2}{c}{Neg. Dev.}&\BSmulticolumn{2}{c}{Neg. Dev. or $<$ 30}&\BSmulticolumn{2}{c}{Low LA}&\BSmulticolumn{2}{c}{Low LA or $<$ 30}\BS\BS") 
to("                         &\BSmulticolumn{2}{c|}{Neg. Dev.}&\BSmulticolumn{2}{c|}{Neg. Dev. or $<$ 30}&\BSmulticolumn{2}{c|}{Low LA}&\BSmulticolumn{2}{c}{Low LA or $<$ 30}\BS\BS") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("                         &\BSmulticolumn{1}{c}{(1)}&\BSmulticolumn{1}{c}{(2)}&\BSmulticolumn{1}{c}{(3)}&\BSmulticolumn{1}{c}{(4)}&\BSmulticolumn{1}{c}{(5)}&\BSmulticolumn{1}{c}{(6)}&\BSmulticolumn{1}{c}{(7)}&\BSmulticolumn{1}{c}{(8)}\BS\BS") 
to("                         &\BSmulticolumn{1}{c}{(1)}&\BSmulticolumn{1}{c|}{(2)}&\BSmulticolumn{1}{c}{(3)}&\BSmulticolumn{1}{c|}{(4)}&\BSmulticolumn{1}{c}{(5)}&\BSmulticolumn{1}{c|}{(6)}&\BSmulticolumn{1}{c}{(7)}&\BSmulticolumn{1}{c}{(8)}\BS\BS") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("                         &\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c}{Const.}&\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c}{Const.}&\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c}{Const.}&\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c}{Const.}\BS\BS") 
to("                         &\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c|}{Const.}&\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c|}{Const.}&\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c|}{Const.}&\BSmulticolumn{1}{c}{Unconst.}&\BSmulticolumn{1}{c}{Const.}\BS\BS") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;


filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("\BSmulticolumn{1}{l}{Observations} & \BSmulticolumn{2}{c}{28074\BSphantom{*}} & \BSmulticolumn{2}{c}{28074\BSphantom{*}} & \BSmulticolumn{2}{c}{25122\BSphantom{*}} & \BSmulticolumn{2}{c}{25122\BSphantom{*}} \BS\BS \BSmulticolumn{1}{l}{Adj. R-squared} & \BSmulticolumn{2}{c}{0.539\BSphantom{*}} & \BSmulticolumn{2}{c}{0.539\BSphantom{*}} & \BSmulticolumn{2}{c}{0.525\BSphantom{*}} & \BSmulticolumn{2}{c}{0.532\BSphantom{*}}\BS\BS") 
to("\BSmulticolumn{1}{l}{Observations} & \BSmulticolumn{2}{c|}{28074\BSphantom{*}} & \BSmulticolumn{2}{c|}{28074\BSphantom{*}} & \BSmulticolumn{2}{c|}{25122\BSphantom{*}} & \BSmulticolumn{2}{c}{25122\BSphantom{*}} \BS\BS \BSmulticolumn{1}{l}{Adj. R-squared} & \BSmulticolumn{2}{c|}{0.539\BSphantom{*}} & \BSmulticolumn{2}{c|}{0.539\BSphantom{*}} & \BSmulticolumn{2}{c|}{0.525\BSphantom{*}} & \BSmulticolumn{2}{c}{0.532\BSphantom{*}}\BS\BS") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

restore; 

} ;

* Table 4: Controlling for Constrained Households and Liquid Asset Holdings ;
if c4 { ;
preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;
** rerunning the same regression 4 times to display four different variables in the same row across 4 columns for table readability; 

** constrained, high liquid wealth;
	*variable we are displaying in the first column;
	
*switch liquid wealth variables with regular constrained (_c) so can stay in same row; 
*then remove from lw local so no collinearity;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	*switch to the variable we are keeping: _c in this case;
	replace `var'_c = `var'_ch_30;
	
	*remove from local (create new local to preserve the original);
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);


* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

*Regression table latex output;
*keep _c version of variables to display; 
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/t4_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Controlling for Constrained Households and Liquid Asset Holdings") 
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} &-1.974***&-2.929***&-0.186   &-1.576* \\  &(0.634)   &(0.888)   &(0.570)   &(0.944) \\ \hline  \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;

* Table 5: Nonlinear Approach to BH ;
if c5 { ;
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if `bh_lm'm_bhc_ev == . | npl_a_50_5m_bhc_ev == . | volainc == . ;

** Col 1: Full FW ; 
eststo A_1, title("Food"): reghdfe ln_psidc_food `totwealth' `mlnpl50qn' `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1 , absorb(unique location_id year) vce(cluster location_id);

* Obs. ;
count if e(sample);
local total_A_1 = r(N);

** Col 2: Full FW + volatility ; 
eststo A_2, title("Food"): reghdfe ln_psidc_food `totwealth' `mlnpl50qn' `vlmlnpl50qn' `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1 , absorb(unique location_id year) vce(cluster location_id);

* Obs. ;
count if e(sample);
local total_A_2 = r(N);

*Regression table latex output;
esttab A_1 A_2 using "`tex_dir'/t5_fin.tex", replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(`totwealth' `inc' `mlnpl50qn' `vlmlnpl50qn')
 varwidth(25) modelwidth(6) booktabs
mtitles nodepvars stats(r2_a, fmt(%5.3f) labels("Adj. R-squared"))
title("Bank Health and Consumption: Nonlinear Approach to Bank Health") 
order(`inc' `mlnpl50qn' `vlmlnpl50qn' `totwealth') collabels(none) label long
substitute("                         &\multicolumn{1}{c}{Food}&\multicolumn{1}{c}{Food}\\" "")
prefoot("\hline \multicolumn{1}{l}{Observations} & \multicolumn{1}{c}{`total_A_1'\phantom{*}} & \multicolumn{1}{c}{`total_A_2'\phantom{*}} \\");

restore;
} ;

* Table 6: Nonlinear BH, Controlling for Constrained HHs ;
if c6 { ;
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;

foreach var in ln_famincrat_re_a totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

foreach v in `mlnpl50qn' `vlmlnpl50qn' { ;
	replace `v'c = `v'ch_30 ;
} ;
local not qn2_npl_a_50_5mbhcevch_30 qn3_npl_a_50_5mbhcevch_30 qn4_npl_a_50_5mbhcevch_30 qn5_npl_a_50_5mbhcevch_30 ;
local mlnpl50qn_lw_mod : list mlnpl50qn_lw_30 - not ;

local not vl_qn2_npl_a_50_5mbhcevch_30 vl_qn3_npl_a_50_5mbhcevch_30 vl_qn4_npl_a_50_5mbhcevch_30 vl_qn5_npl_a_50_5mbhcevch_30;
local vlmlnpl50qn_lw_mod : list vlmlnpl50qn_lw_30 - not ;

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food 
`mlnpl50qn_c' `vlmlnpl50qn_c' ln_famincrat_re_a_c totwr_100_re_a_c 
`mlnpl50qn_lw_mod' `vlmlnpl50qn_lw_mod' 
`inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);


** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

foreach v in `mlnpl50qn' `vlmlnpl50qn' { ;
	replace `v'c = `v'cl_30 ;
} ;
local not qn2_npl_a_50_5mbhcevcl_30 qn3_npl_a_50_5mbhcevcl_30 qn4_npl_a_50_5mbhcevcl_30 qn5_npl_a_50_5mbhcevcl_30 ;
local mlnpl50qn_lw_mod : list mlnpl50qn_lw_30 - not ;

local not vl_qn2_npl_a_50_5mbhcevcl_30 vl_qn3_npl_a_50_5mbhcevcl_30 vl_qn4_npl_a_50_5mbhcevcl_30 vl_qn5_npl_a_50_5mbhcevcl_30;
local vlmlnpl50qn_lw_mod : list vlmlnpl50qn_lw_30 - not ;


eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food 
`mlnpl50qn_c' `vlmlnpl50qn_c' ln_famincrat_re_a_c totwr_100_re_a_c 
`mlnpl50qn_lw_mod' `vlmlnpl50qn_lw_mod' 
`inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

foreach v in `mlnpl50qn' `vlmlnpl50qn' { ;
	replace `v'c = `v'uh_30 ;
} ;
local not qn2_npl_a_50_5mbhcevuh_30 qn3_npl_a_50_5mbhcevuh_30 qn4_npl_a_50_5mbhcevuh_30 qn5_npl_a_50_5mbhcevuh_30 ;
local mlnpl50qn_lw_mod : list mlnpl50qn_lw_30 - not ;

local not vl_qn2_npl_a_50_5mbhcevuh_30 vl_qn3_npl_a_50_5mbhcevuh_30 vl_qn4_npl_a_50_5mbhcevuh_30 vl_qn5_npl_a_50_5mbhcevuh_30;
local vlmlnpl50qn_lw_mod : list vlmlnpl50qn_lw_30 - not ;

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food 
`mlnpl50qn_c' `vlmlnpl50qn_c' ln_famincrat_re_a_c totwr_100_re_a_c 
`mlnpl50qn_lw_mod' `vlmlnpl50qn_lw_mod' 
`inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

foreach v in `mlnpl50qn' `vlmlnpl50qn' { ;
	replace `v'c = `v'ul_30 ;
} ;
local not qn2_npl_a_50_5mbhcevul_30 qn3_npl_a_50_5mbhcevul_30 qn4_npl_a_50_5mbhcevul_30 qn5_npl_a_50_5mbhcevul_30 ;
local mlnpl50qn_lw_mod : list mlnpl50qn_lw_30 - not ;

local not vl_qn2_npl_a_50_5mbhcevul_30 vl_qn3_npl_a_50_5mbhcevul_30 vl_qn4_npl_a_50_5mbhcevul_30 vl_qn5_npl_a_50_5mbhcevul_30;
local vlmlnpl50qn_lw_mod : list vlmlnpl50qn_lw_30 - not ;


eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food 
`mlnpl50qn_c' `vlmlnpl50qn_c' ln_famincrat_re_a_c totwr_100_re_a_c 
`mlnpl50qn_lw_mod' `vlmlnpl50qn_lw_mod' 
`inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

*Regression table latex output;
*keep _c version of variables to display; 
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/t6_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `mlnpl50qn_c' `vlmlnpl50qn_c' totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Nonlinear Approach to Bank Health \\ Controlling for Constrained Households") 
order(ln_famincrat_re_a_c `mlnpl50qn_c' `vlmlnpl50qn_c' totwr_100_re_a_c) collabels(none) label long  
prefoot("\hline  \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");
restore;

} ;

* Table 7: Alt Def of Constrained (5% and < 30 constrainted) ;
if c7 { ;

preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;

foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ch_30_5;
	
	local not `var'_ch_30_5;
	local `l'_lw_mod : list `l'_lw_30_5 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);


* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_cl_30_5;
	
	local not `var'_cl_30_5;
	local `l'_lw_mod : list `l'_lw_30_5 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_uh_30_5;
	
	local not `var'_uh_30_5;
	local `l'_lw_mod : list `l'_lw_30_5 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ul_30_5;
	
	local not `var'_ul_30_5;
	local `l'_lw_mod : list `l'_lw_30_5 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcul ;


* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/t7_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("$\geq$5\% Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Controlling for Constrained, Liquid Assets\\ Alternative Definition of Constrained") 
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} &-2.455***&-2.952***&-0.170   &-1.712*  \\    &(0.771)   &(0.953)   &(0.608)   &(1.003) \\ \hline  \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;
} ;

* APPENDIX TABLES ;
if apx { ;
* Table A.1: Summary Statistics ;
if a1 { ;
*locals for sum stat tables;
local var_int rfood bpp_lvl altc1 famincrat;
local bhvars npl_a_50_ev npl_a_50_5m_bhc_ev cam_wa_ev ;
local wvars totwr lfwr_combo_re_a l_liqw_combo ;
local macro_controls hp_ev_w1_100  bartik_ebev_w1_100;
local hh_controls ageh famsize single m_se m_de ;
local other share_constrained_30 share_constrained_30_5 share_owner ;

local allvars_hw `var_int' `bhvars' `macro_controls' `hh_controls' `other' ;
local allvars_fw `var_int' `bhvars' `macro_controls' `hh_controls' `other' `wvars' ;

local all_npl_a_hw rfood famincrat npl_a_50_ev npl_a_50_5m_bhc_ev `macro_controls' `hh_controls' ;
local all_npl_a_fw rfood famincrat npl_a_50_ev npl_a_50_5m_bhc_ev `wvars' `macro_controls' `hh_controls' ;

////////////////////////
foreach samp in hw hw_fw { ;
	if "`samp'"=="hw" local x hw ;
	if "`samp'"=="hw_fw" local x fw ;
	
use `data_dir'/`dataname'_`samp'_lw_new_30_15.dta, clear;
keep if every_dum_npl_a_bhc5rf == 1 ;
drop if volainc == . ;

qui reghdfe ln_psidc_food `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1, absorb(unique location_id year) vce(cluster location_id);
keep if e(sample) ;

if "`samp'"=="hw" { ;
keep unique year location_id m_se m_de ageh famsize rfood famincrat hp_ev_w1 
	bartik_ebev_w1 constrained_30_re_a constrained_30_5_re_a every_dum_re_a 
	every_dum_cam  every_dum_npl_a_bhc5rf npl_a_50_ev npl_a_50_5m_bhc_ev cam_wa_ev renter 
	rhfurn rhrec rvaca rschool rcloth rfood 
	`inc' `hh_control' `macro_control';
} ;

if "`samp'"=="hw_fw" { ;
keep unique year location_id m_se m_de ageh famsize rfood famincrat hp_ev_w1 
	bartik_ebev_w1 constrained_30_re_a constrained_30_5_re_a every_dum_re_a 
	every_dum_cam  every_dum_npl_a_bhc5rf npl_a_50_ev npl_a_50_5m_bhc_ev cam_wa_ev renter 
	rhfurn rhrec rvaca rschool rcloth rfood 
	l_liqw_combo lfwr_combo_re_a totwr_100 
	`inc' `hh_control' `macro_control';
} ;

la var rfood "Food Consumption [\textdollar]";
la var famincrat "Income [\textdollar]";


*'single' variable i.e. missing category out of the m_se/m_de group;
g single=0 ;
replace single=1 if m_se==0 & m_de==0;
la var single "Marital Status: Single";

* display BH not divided by 100, so multiply by 100 ;
replace npl_a_50_ev = npl_a_50_ev*100 ;
replace npl_a_50_5m_bhc_ev = npl_a_50_5m_bhc_ev*100 ;

*percents;
foreach var in hp_ev_w1 bartik_ebev_w1  {;
	g `var'_100 = `var'*100;
};
la var hp_ev_w1_100 "House Price Growth [\%]";
la var bartik_ebev_w1_100 "Bartik Empl. Growth [\%]";

if "`samp'"=="hw_fw" { ;
	foreach v in totwr { ;
		gen `v' = `v'_100*100 ;
	} ;

la var totwr "Total Wealth/Income" ;
la var lfwr_combo_re_a "Liquid Assets/Income";
la var l_liqw_combo "Liquid Assets [\textdollar]" ;
} ;

// share constrained neg dev and neg dev 5% 
*get exact sample for constraint including dropping singletons (HH and location) created by missings in constraint;
*this is done automatically when you run regressions, but want to capture exact sample for sum stats;
* like the every_dum process for non-constraint variables in cleaning section of maindata.do ;
foreach var in _30 _30_5 {;

	local v `var' ;
		
	*create new sample dummy variable; 
	g every_dum_re_a`var' = 1 if constrained`v'_re_a != . & every_dum_re_a == 1;
		
	g x = 1;
	while x != 0 {;

		*count observations by unique and replace sample dummy with missing if fewer than 2 observations; 
		bysort unique: egen every_dum_re_a_obs = total(every_dum_re_a`var');
		replace every_dum_re_a`var' = . if every_dum_re_a_obs<2;
			
		*do the same with observations by location; 
		bys location_id: egen location_re_a_obs = total(every_dum_re_a`var');
		replace every_dum_re_a`var' = . if location_re_a_obs < 2;
			
		*another loop? did dropping location singletons create more HH singletons?;
		drop every_dum_re_a_obs;
		bysort unique: egen every_dum_re_a_obs = total(every_dum_re_a`var');
		count if every_dum_re_a_obs == 1;
		replace x = r(N);
		
		drop every_dum_re_a_obs location_re_a_obs ;
	};
		
	drop x;
};

* calculate the share constrained ;
bys year: egen constrained_count_30 = total(constrained_30_re_a) if every_dum_re_a_30 == 1;
bys year: egen constrained_count_30_5 = total(constrained_30_5_re_a) if every_dum_re_a_30_5 == 1 ;
	
bys year: egen total_cons_count_30 = count(constrained_30_re_a) if every_dum_re_a_30 == 1;
bys year: egen total_cons_count_30_5 = count(constrained_30_5_re_a) if every_dum_re_a_30_5 == 1;

g share_constrained_30 = (constrained_count_30 / total_cons_count_30) * 100 if every_dum_re_a_30 == 1;
g share_constrained_30_5 = (constrained_count_30_5 / total_cons_count_30_5) * 100 if every_dum_re_a_30_5 == 1;

la var share_constrained_30 "Const. HHs (Neg. Dev. or $<$ 30) [\%]"; 
la var share_constrained_30_5 "Const. HHs ($\geq$5\% Neg. Dev. or $<$ 30) [\%]"; 


gen owner = 1 if renter == 0 ;
replace owner = 0 if renter == 1 ;

* share of renters, owners ;
foreach ver in renter owner { ;
bys year: egen `ver'_count = total(`ver') ;
bys year: egen total_`ver'_count = count(`ver') ;
g share_`ver' = (`ver'_count/total_`ver'_count)*100 ;
} ;
la var share_renter "Percent of Renters" ;
la var share_owner "Homeowners [\%]" ;

* Alt Consumption measure ;
preserve ;

egen altc1 = rowtotal(rhfurn rhrec rvaca rschool rcloth rfood), miss ;
la var altc1 "Alt PSID Consumption [\textdollar]" ;
drop if year < 2005 | altc1 == .;

gen ln_psidc_altc1 = ln(altc1) ;

qui reghdfe ln_psidc_altc1 `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1, absorb(unique location_id year) vce(cluster location_id);
keep if e(sample) ;

keep unique year altc1 ;
tempfile altc ;
save `altc', replace ;
restore ;

merge 1:1 unique year using `altc', nogen ;

*BPP;
preserve ;
use unique year bpp volainc every_dum_npl_a_bhc5rf `inc' `hh_control' `macro_control' location_id using `data_dir'/`dataname'_`samp'_bpp_new_30_15.dta, clear ;
drop if volainc == . ;
*get non-log version of bpp for summary statistics;
g bpp_lvl = exp(bpp);
la var bpp_lvl "Imputed Nondurable Consumption [\textdollar]";

qui reghdfe bpp `inc' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf ==1, absorb(unique location_id year) vce(cluster location_id);
keep if e(sample) ;

keep unique year bpp_lvl ;
tempfile bppdta ;
save `bppdta', replace ;
restore ;

merge 1:1 unique year using `bppdta', nogen ;

* CAM ;
preserve ;
use unique year cam_wa_ev volainc every_dum_cam ln_psidc_food `inc' `hh_control' `macro_control' location_id using `data_dir'/`dataname'_`samp'_lw_new_30_15.dta, clear ;
drop if volainc == . ;
la var cam_wa_ev "CAMELS"; 

qui reghdfe ln_psidc_food `inc' `hh_control' `macro_control'
if every_dum_cam ==1, absorb(unique location_id year) vce(cluster location_id);
keep if e(sample) ;

keep unique year cam_wa_ev ;
tempfile camdta ;
save `camdta', replace ;
restore ;

merge 1:1 unique year using `camdta', nogen ;

* summarize ;
estpost su `allvars_`x'', d;

if "`samp'"=="hw" { ;
	eststo full;
} ;
if "`samp'"=="hw_fw" { ;
	eststo wealth;
} ;

} ;

local outname "a1_fin" ;
esttab full wealth using `tex_dir'/`outname'.tex,
cells("mean(fmt(%12.2gc)) p50 sd min max count(fmt(%9.0gc))") 
posthead(& \multicolumn{6}{c}{Full Sample} & \multicolumn{6}{c}{Wealth Sample} \\ \cmidrule(lr){2-7} \cmidrule(lr){8-13} Variable &Mean &Median &Std. Dev &Min. &Max. &N &Mean &Median &Std. Dev &Min. &Max. &N \\ \hline) 
title("Summary Statistics") 
label replace noobs nonumbers booktabs 
substitute("                    &        mean&         p50&          sd&         min&         max&       count&        mean&         p50&          sd&         min&         max&       count\\" "")  ;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("                    &\BSmulticolumn{6}{c}{}                                                         &\BSmulticolumn{6}{c}{}                                                         \BS\BS") 
to("") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("\BSbegin{tabular}{l*{2}{cccccc}}") 
to("\BSbegin{tabular}{lcccccc|cccccc}") replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

} ;

* Table A.2: Correlation of Non-Demographic Regressors ;
if a2 { ;

preserve ;
local width "p{0.8cm}" ;

use `data_dir'/`dataname'_hw_lw_new_30_15.dta, clear;
keep if every_dum_npl_a_bhc5rf == 1 & volainc != . ;

* Full Sample ;
estpost corr `inc' `macro_control' 
	npl_a_50_ev vl_npl_a_50_ev npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev, matrix;
eststo corrs ;

local outname "a2_hw_fin" ;
esttab corrs using `tex_dir'/`outname'.tex, nonote noobs nonum booktabs 
unstack compress nogaps label replace ;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("                &Log Income         &House Price Growth         &Bartik Empl. Growth         &    NPL50         &Inc. Volatility x NPL50         & ML NPL50         &Inc. Volatility x ML NPL50         \BS\BS")
to("&\BSmulticolumn{1}{`width'}{Log Income} & \BSmulticolumn{1}{`width'}{House Price Growth} & \BSmulticolumn{1}{`width'}{Bartik Empl. Growth} & \BSmulticolumn{1}{`width'}{NPL50} & \BSmulticolumn{1}{`width'}{Inc. Volatility x NPL50} & \BSmulticolumn{1}{`width'}{ML NPL50} & \BSmulticolumn{1}{`width'}{Inc. Volatility x ML NPL50} \BS\BS")  replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

* Wealth Sample ;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
keep if every_dum_npl_a_bhc5rf == 1 & volainc != . ;
					
estpost corr `inc' `macro_control' 
	npl_a_50_ev vl_npl_a_50_ev npl_a_50_5m_bhc_ev vl_npl_a_50_5mbhcev
	`totwealth' `totasset' `totdebt', matrix ;
eststo corrs ;

local outname "a2_hw_fw_fin" ;
esttab corrs using `tex_dir'/`outname'.tex, nonote noobs nonum booktabs 
unstack compress nogaps label replace;

filefilter `tex_dir'/`outname'.tex `tex_dir'/`outname'_1.tex, 
from("                &Log Income         &House Price Growth         &Bartik Empl. Growth         &    NPL50         &Inc. Volatility x NPL50         & ML NPL50         &Inc. Volatility x ML NPL50         &Total Wealth/Income         &Total Assets/Income         &Total Debt/Income         \BS\BS")
to("&\BSmulticolumn{1}{`width'}{Log Income} & \BSmulticolumn{1}{`width'}{House Price Growth} & \BSmulticolumn{1}{`width'}{Bartik Empl. Growth} & \BSmulticolumn{1}{`width'}{NPL50} & \BSmulticolumn{1}{`width'}{Inc. Volatility x NPL50} & \BSmulticolumn{1}{`width'}{ML NPL50} & \BSmulticolumn{1}{`width'}{Inc. Volatility x ML NPL50} & \BSmulticolumn{1}{`width'}{Total Wealth /Income} &\BSmulticolumn{1}{`width'}{Total Assets /Income} & \BSmulticolumn{1}{`width'}{Total Debt /Income} \BS\BS")  replace;
copy `tex_dir'/`outname'_1.tex `tex_dir'/`outname'.tex, replace ;
erase `tex_dir'/`outname'_1.tex ;

restore ;
} ;

* Table A.4: BH Based on Real Estate Nonperforming Loans (RENPL50) ;
if a4 { ;
preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if re_a_50_5m_bhc_ev == . ;

foreach var in ln_famincrat_re_a `bh_lm'm_bhc_ev `vola' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bh_lm'm_bhc_ev" local l bh_vars_lm ;
	if "`var'" == "`vola'" local l vola;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food `bh_lm'm_bhc_ev_c `vola'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bh_vars_lm_lw_mod' `vola_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_re_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest `bh_lm'm_bhc_ev_c + `vola'_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bh_lm'm_bhc_ev `vola' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bh_lm'm_bhc_ev" local l bh_vars_lm ;
	if "`var'" == "`vola'" local l vola;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food `bh_lm'm_bhc_ev_c `vola'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bh_vars_lm_lw_mod' `vola_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_re_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest `bh_lm'm_bhc_ev_c + `vola'_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bh_lm'm_bhc_ev `vola' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bh_lm'm_bhc_ev" local l bh_vars_lm ;
	if "`var'" == "`vola'" local l vola;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food `bh_lm'm_bhc_ev_c `vola'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bh_vars_lm_lw_mod' `vola_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_re_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest `bh_lm'm_bhc_ev_c + `vola'_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bh_lm'm_bhc_ev `vola' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bh_lm'm_bhc_ev" local l bh_vars_lm ;
	if "`var'" == "`vola'" local l vola;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food `bh_lm'm_bhc_ev_c `vola'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bh_vars_lm_lw_mod' `vola_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_re_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest `bh_lm'm_bhc_ev_c + `vola'_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a4_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bh_lm'm_bhc_ev_c `vola'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Bank Health Based on Real Estate Nonperforming Loans") 
order(ln_famincrat_re_a_c `bh_lm'm_bhc_ev_c `vola'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} &-2.101***&-3.458***&-0.372   &-1.751*  \\ &(0.768)   &(0.960)   &(0.672)   &(1.042)   \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;

* Table A.5: ML Banks Have 10% Share (or More) of Local Deposits (10% restriction) ;
if a5 { ;
preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;

foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc10rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest `bhnpl_lm'm_bhc_ev_c + `volanpl'_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc10rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest `bhnpl_lm'm_bhc_ev_c + `volanpl'_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc10rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest `bhnpl_lm'm_bhc_ev_c + `volanpl'_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc10rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest `bhnpl_lm'm_bhc_ev_c + `volanpl'_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a5_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Alternative Def. ML Banks \\ Parent Banks with 10\% or Less of Overall Deposits in Location") 
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect}    &-2.148***&-3.490***&-0.488   &-1.925*  \\ &(0.756)   &(0.952)   &(0.640)   &(1.025)   \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;

* Table A.6: Alt Def of ML Banks: Parent Banks with 10% or Less of Overall Deposits in Location (M10) ;
if a6 { ;
preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_10m_bhc_ev == . ;

foreach var in ln_famincrat_re_a `bhnpl10_lm'm_bhc_ev `volanpl10' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl10_lm'm_bhc_ev" local l bhnpl10_vars_lm ;
	if "`var'" == "`volanpl10'" local l volanpl10;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food `bhnpl10_lm'm_bhc_ev_c `volanpl10'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl10_vars_lm_lw_mod' `volanpl10_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest `bhnpl10_lm'm_bhc_ev_c + `volanpl10'_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl10_lm'm_bhc_ev `volanpl10' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl10_lm'm_bhc_ev" local l bhnpl10_vars_lm ;
	if "`var'" == "`volanpl10'" local l volanpl10;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food `bhnpl10_lm'm_bhc_ev_c `volanpl10'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl10_vars_lm_lw_mod' `volanpl10_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest `bhnpl10_lm'm_bhc_ev_c + `volanpl10'_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl10_lm'm_bhc_ev `volanpl10' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl10_lm'm_bhc_ev" local l bhnpl10_vars_lm ;
	if "`var'" == "`volanpl10'" local l volanpl10;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food `bhnpl10_lm'm_bhc_ev_c `volanpl10'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl10_vars_lm_lw_mod' `volanpl10_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest `bhnpl10_lm'm_bhc_ev_c + `volanpl10'_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl10_lm'm_bhc_ev `volanpl10' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl10_lm'm_bhc_ev" local l bhnpl10_vars_lm ;
	if "`var'" == "`volanpl10'" local l volanpl10;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food `bhnpl10_lm'm_bhc_ev_c `volanpl10'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl10_vars_lm_lw_mod' `volanpl10_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest `bhnpl10_lm'm_bhc_ev_c + `volanpl10'_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a6_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl10_lm'm_bhc_ev_c `volanpl10'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Alternative Definition ML Banks \\ ML Banks Have 10\% Share (or More) of Local Deposits") 
order(ln_famincrat_re_a_c `bhnpl10_lm'm_bhc_ev_c `volanpl10'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect}  &-2.483***&-3.131***&-0.610   &-2.384** \\  &(0.707)   &(1.002)   &(0.634)   &(1.029)   \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;

* Table A.7: Alternative (Supervisory-based) BH Measure (Camels) ;
if a7 { ;
preserve;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;

foreach var in ln_famincrat_cam cam_wa_ev vl_cam_wa_ev totwr_100_cam { ;

	if "`var'" == "ln_famincrat_cam" local l inc_cam;
	if "`var'" == "totwr_100_cam" local l totwealth_cam;
	if "`var'" == "cam_wa_ev" local l bh_vars_cam;
	if "`var'" == "vl_cam_wa_ev" local l vola_cam;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food 
cam_wa_ev_c vl_cam_wa_ev_c ln_famincrat_cam_c totwr_100_cam_c
`bh_vars_cam_lw_mod' `vola_cam_lw_mod' `inc_cam_lw_mod' `totwealth_cam_lw_mod' 
`hh_control' `macro_control'
if every_dum_cam == 1 , absorb(unique year location_id) vce(cluster location_id);


* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest cam_wa_ev_c+vl_cam_wa_ev_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_cam cam_wa_ev vl_cam_wa_ev totwr_100_cam { ;

	if "`var'" == "ln_famincrat_cam" local l inc_cam;
	if "`var'" == "totwr_100_cam" local l totwealth_cam;
	if "`var'" == "cam_wa_ev" local l bh_vars_cam;
	if "`var'" == "vl_cam_wa_ev" local l vola_cam;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food 
cam_wa_ev_c vl_cam_wa_ev_c ln_famincrat_cam_c totwr_100_cam_c
`bh_vars_cam_lw_mod' `vola_cam_lw_mod' `inc_cam_lw_mod' `totwealth_cam_lw_mod' 
`hh_control' `macro_control'
if every_dum_cam == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest cam_wa_ev_c+vl_cam_wa_ev_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_cam cam_wa_ev vl_cam_wa_ev totwr_100_cam { ;

	if "`var'" == "ln_famincrat_cam" local l inc_cam;
	if "`var'" == "totwr_100_cam" local l totwealth_cam;
	if "`var'" == "cam_wa_ev" local l bh_vars_cam;
	if "`var'" == "vl_cam_wa_ev" local l vola_cam;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food 
cam_wa_ev_c vl_cam_wa_ev_c ln_famincrat_cam_c totwr_100_cam_c
`bh_vars_cam_lw_mod' `vola_cam_lw_mod' `inc_cam_lw_mod' `totwealth_cam_lw_mod' 
`hh_control' `macro_control'
if every_dum_cam == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest cam_wa_ev_c+vl_cam_wa_ev_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_cam cam_wa_ev vl_cam_wa_ev totwr_100_cam { ;

	if "`var'" == "ln_famincrat_cam" local l inc_cam;
	if "`var'" == "totwr_100_cam" local l totwealth_cam;
	if "`var'" == "cam_wa_ev" local l bh_vars_cam;
	if "`var'" == "vl_cam_wa_ev" local l vola_cam;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food 
cam_wa_ev_c vl_cam_wa_ev_c ln_famincrat_cam_c totwr_100_cam_c
`bh_vars_cam_lw_mod' `vola_cam_lw_mod' `inc_cam_lw_mod' `totwealth_cam_lw_mod' 
`hh_control' `macro_control'
if every_dum_cam == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest cam_wa_ev_c+vl_cam_wa_ev_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a7_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(cam_wa_ev_c vl_cam_wa_ev_c ln_famincrat_cam_c totwr_100_cam_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Alternative (Supervisory-based) Bank Health Measure") 
order(ln_famincrat_cam_c cam_wa_ev_c vl_cam_wa_ev_c totwr_100_cam_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect}    &-0.070***&-0.121***&-0.004   &-0.033   \\ &(0.021)   &(0.026)   &(0.019)   &(0.029)   \\ \hline  \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;

* Table A.8: Alternative Consumption Measure, 2005-2015 ;
if a8 { ;
use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;
drop if year < 2005 ;

local x "HFurn + HRec + Vaca + School + Cloth + Food" ;
local i "1" ;

preserve;

foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_altc1 `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_altc1 `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_altc1 `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_altc1 `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none) ;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a8_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Alternative Consumption Measure, 2005-2015")
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect}    &-1.555   &-2.715** &-1.413   &-1.286   \\  &(1.043)   &(1.065)   &(0.988)   &(1.202)   \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;

* Table A.9: Imputed Nondurables Consumption (BPP) ;
if a9 { ;
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
/*sum bpp ;

use `data_dir'/main_dataset_revision_hmr_v4temp_hw_fw_lw_new_30_15.dta, clear;
sum bpp ; */
drop if npl_a_50_5m_bhc_ev == . ;

foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe bpp `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe bpp `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe bpp `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm ;
	if "`var'" == "`volanpl'" local l volanpl;
			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe bpp `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none)
;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a9_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: Imputed Nondurables Consumption") 
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} &-1.050** &-1.003*  &-0.163   &-0.934*  \\ &(0.445)   &(0.541)   &(0.427)   &(0.544)   \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\")
;

restore;

} ;

* Table A.10: State x Year Fixed Effects ;
if a10 { ;
preserve;

use `data_dir'/`dataname'_hw_fw_lw_new_30_15.dta, clear;
drop if npl_a_50_5m_bhc_ev == . ;

foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm;
	if "`var'" == "`volanpl'" local l volanpl;
	
	replace `var'_c = `var'_ch_30;
	
	local not `var'_ch_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1ch, title("Const., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id styr) vce(cluster location_id);

* Total BH effect ;
qui sum avgsqdev if e(sample) ;
local yvol "`r(mean)'" ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcch ;

** constrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_cl_30;
	
	local not `var'_cl_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1cl, title("Const., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id styr) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lccl ;

** unconstrained, high liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;

	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_uh_30;
	
	local not `var'_uh_30;
	local `l'_lw_mod : list `l'_lw_30 - not;
	
};

eststo C_1uh, title("Unconst., High LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id styr) vce(cluster location_id);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcuh ;

** unconstrained, low liquid wealth;
foreach var in ln_famincrat_re_a `bhnpl_lm'm_bhc_ev `volanpl' totwr_100_re_a {;
	if "`var'" == "ln_famincrat_re_a" local l inc;
	if "`var'" == "totwr_100_re_a" local l totwealth;
	if "`var'" == "`bhnpl_lm'm_bhc_ev" local l bhnpl_vars_lm;
	if "`var'" == "`volanpl'" local l volanpl;

			
	replace `var'_c = `var'_ul_30;
	
	local not `var'_ul_30;
	local `l'_lw_mod : list `l'_lw_30 - not;

};
eststo C_1ul, title("Unconst., Low LA"): reghdfe ln_psidc_food `bhnpl_lm'm_bhc_ev_c `volanpl'_c ln_famincrat_re_a_c totwr_100_re_a_c 
`bhnpl_vars_lm_lw_mod' `volanpl_lw_mod' `inc_lw_mod' `totwealth_lw_mod' `hh_control' `macro_control'
if every_dum_npl_a_bhc5rf == 1 , absorb(unique year location_id styr) vce(cluster location_id);

*R2;
local r_C_1 = string(e(r2_a), "%4.3f");

* Obs. ;
count if e(sample);
local total_C_1 = r(N);

* Total BH effect ;
lincomest npl_a_50_5m_bhc_ev_c + vl_npl_a_50_5mbhcev_c*`yvol' ;
eststo lcul ;

* total BH effect stars ;
esttab lcch lccl lcuh lcul, starlevel(* 0.10 ** 0.05 *** 0.01)
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) noobs 
varwidth(25) modelwidth(6) booktabs mtitles nodepvars collabels(none) ;

* latex output;
esttab C_1ch C_1cl C_1uh C_1ul using "`tex_dir'/a10_fin.tex", 
replace starlevels(* 0.10 ** 0.05 *** 0.01) alignment(S[table-format=2.5, table-align-text-post=false])
cells(b(star fmt(%5.3fc) label("")) se(par fmt(%5.3fc) label(""))) 
keep(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c totwr_100_re_a_c)
noobs varwidth(25) modelwidth(6) booktabs
mtitles nodepvars mgroups("Neg. Dev. or $<$ 30", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
title("Bank Health and Consumption: State x Year Fixed Effects") 
order(ln_famincrat_re_a_c `bhnpl_lm'm_bhc_ev_c `volanpl'_c ltotwr_100_re_a_c) collabels(none) label long  
prefoot("\hline \multicolumn{1}{l}{\textit{Memo:} Total BH Effect} &-2.132** &-2.775***&-0.086   &-1.270   \\  &(0.940)   &(1.037)   &(0.812)   &(1.094)   \\ \hline \multicolumn{1}{l}{Observations} & \multicolumn{4}{c}{`total_C_1'\phantom{*}} \\ \multicolumn{1}{l}{Adj. R-squared} & \multicolumn{4}{c}{`r_C_1'\phantom{*}}  \\");

restore;
} ;
} ;
